//
//  ViewController.m
//  10-易车作业
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import "YMJTableViewCell.h"
#import "SecondTableViewCell.h"
#import "ModelBaseDic.h"
#import "Data.h"
#import "CycleImage.h"
#import "List.h"
#import "ThreeStringModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArrayBaseDic;
@property (nonatomic, strong) UIView *myView;
@property (nonatomic, strong) UIImageView *cycleView;
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic ,strong) NSMutableArray *cycleArrayBaseDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[YMJTableViewCell class] forCellReuseIdentifier:@"1"];
    [self.tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"2"];
    self.tableView.rowHeight = 111.25;
    // 解析数据
    [self createData];
    // 轮播图
    [self createCycleView];
    
    
    
}
- (void)createCycleView {
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 291)];
    _myView.backgroundColor = [UIColor purpleColor];
    self.tableView.tableHeaderView = _myView;
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 414, 202)];
    self.scroll.contentSize = CGSizeMake(414 * (_cycleArrayBaseDic.count + 2), 0);
    self.scroll.contentOffset = CGPointMake(414, 0);
    self.scroll.backgroundColor = [UIColor darkGrayColor];
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    [self.myView addSubview:_scroll];
    for (NSInteger i = 0; i < _cycleArrayBaseDic.count; i++) {
        self.cycleView = [[UIImageView alloc]initWithFrame:CGRectMake((i + 1) * 414, 0, 414, 202)];
        [self.scroll addSubview:_cycleView];
        NSURL *url = [NSURL URLWithString:[_cycleArrayBaseDic[i] picCover]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        _cycleView.image = [UIImage imageWithData:data];
    }
    self.cycleView = [[UIImageView alloc] initWithFrame:CGRectMake((_cycleArrayBaseDic.count + 1) * 414, 0, 414, 202)];
    [self.scroll addSubview:_cycleView];
    NSURL *url = [NSURL URLWithString:[[_cycleArrayBaseDic firstObject] picCover]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    _cycleView.image = [UIImage imageWithData:data];
    self.cycleView = [[UIImageView alloc] initWithFrame:CGRectMake(0 * 414, 0, 414, 202)];
    [self.scroll addSubview:_cycleView];
    NSURL *url1 = [NSURL URLWithString:[[_cycleArrayBaseDic lastObject] picCover]];
    NSData *data1 = [NSData dataWithContentsOfURL:url1];
    _cycleView.image = [UIImage imageWithData:data1];
    
}
- (void)createData {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
//    NSLog(@"%@",dic);
    NSDictionary *dataDic = [dic objectForKey:@"data"];
//    NSLog(@"%@",dataDic);
    //
    NSArray *cycleImageArray = [dataDic objectForKey:@"cycleImage"];
//    NSLog(@"%@",cycleImageArray);
    NSArray *listArray = [dataDic objectForKey:@"list"];
//    NSLog(@"%@",listArray);
    
    
//    List *list = [List alloc] initWithpicCover:<#(NSString *)#> title:<#(NSString *)#>
    // cell数组
    self.listArrayBaseDic = [NSMutableArray array];
    for (NSDictionary *dic in listArray) {
        if (dic[@"type"] != [NSNumber numberWithInteger:3]) {
            List *list = [[List alloc] init];
            [list setValuesForKeysWithDictionary:dic];
            [_listArrayBaseDic addObject:list];
            NSLog(@"不是");
        } else {
            ThreeStringModel *threeModel = [[ThreeStringModel alloc] initWithPicCover:dic[@"picCover"]];
            [_listArrayBaseDic addObject:threeModel];
             NSLog(@"%@",threeModel);
            NSLog(@"是");
        }
      
        NSLog(@"-----%@-----",dic[@"picCover"]);
        NSLog(@"-----%@-----",dic[@"type"]);
    }
   
    // 轮播图数组
    self.cycleArrayBaseDic = [NSMutableArray array];
    for (NSDictionary *dic in cycleImageArray) {
        CycleImage *cycle = [[CycleImage alloc] init];
        [cycle setValuesForKeysWithDictionary:dic];
        [_cycleArrayBaseDic addObject:cycle];
    }
    
  
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_scroll.contentOffset.x == 414 * (_cycleArrayBaseDic.count + 1)) {
        _scroll.contentOffset = CGPointMake(414, 0);
    } else if (_scroll.contentOffset.x == 0) {
        _scroll.contentOffset = CGPointMake(414 *_cycleArrayBaseDic.count, 0);
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArrayBaseDic.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YMJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    SecondTableViewCell *secCell = [tableView dequeueReusableCellWithIdentifier:@"2"];
    cell.backgroundColor = [UIColor whiteColor];
    if ([_listArrayBaseDic[indexPath.row] isKindOfClass:[List class]]) {
        cell.list = _listArrayBaseDic[indexPath.row];
        return cell;
    } else {
        secCell.threeStringModel = _listArrayBaseDic[indexPath.row];
        return secCell;
    }
    

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
